package cn.rongdao.jrkabao.ui.setting;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRSharedPreferences;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.switchbutton.CustomSwitch;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;

/**
 * Created by jack on 2017/11/13
 */

public class SelectEnvironmentActivity extends BaseAutoFinishActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, SelectEnvironmentActivity.class);
    }

    @BindView(R.id.ase_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.ase_switch)
    CustomSwitch customSwitch;
    @BindView(R.id.ase_rg_group)
    RadioGroup radioGroup;
    @BindView(R.id.ase_rb_test)
    RadioButton rbTest;
    @BindView(R.id.ase_rb_pre)
    RadioButton rbPre;
    @BindView(R.id.ase_rb_formal)
    RadioButton rbFormal;
    @BindView(R.id.ase_et_link)
    EditText etLink;
    @BindView(R.id.ase_btn_go)
    Button btnGo;


    @Override
    protected int layoutRes() {
        return R.layout.activity_select_environment;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitleBar();

        if (!FRString.isEmpty(FRSharedPreferences.getStringNotClear(SPConstant.IS_USE_CLOUD_API, ""))) {
            customSwitch.setState(true);
        }

        if (FRSharedPreferences.getStringNotClear(SPConstant.IS_TEST_OR_PRE_OR_FORMAL, "test").equals("test")) {
            rbTest.setChecked(true);
        } else if (FRSharedPreferences.getStringNotClear(SPConstant.IS_TEST_OR_PRE_OR_FORMAL, "pre").equals("pre")) {
            rbPre.setChecked(true);
        } else {
            rbFormal.setChecked(true);
        }

        initListener();
    }

    private void initListener() {
        customSwitch.setOnCheckedChangeListener(new CustomSwitch.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CustomSwitch customeSwitch, boolean checked) {
                if (checked) {
                    FRSharedPreferences.setStringNotClear(SPConstant.IS_USE_CLOUD_API, "yes");
                } else {
                    FRSharedPreferences.setStringNotClear(SPConstant.IS_USE_CLOUD_API, "");
                }
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == rbTest.getId()) {
                    FRSharedPreferences.setStringNotClear(SPConstant.IS_TEST_OR_PRE_OR_FORMAL, "test");
                    rbTest.setChecked(true);
                } else if (checkedId == rbPre.getId()) {
                    FRSharedPreferences.setStringNotClear(SPConstant.IS_TEST_OR_PRE_OR_FORMAL, "pre");
                    rbPre.setChecked(true);
                } else {
                    FRSharedPreferences.setStringNotClear(SPConstant.IS_TEST_OR_PRE_OR_FORMAL, "formal");
                    rbFormal.setChecked(true);
                }
            }
        });

        btnGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = etLink.getText().toString().trim();
                WebViewActivity.start(thisActivity(), FRString.isEmpty(url) ? "file:///android_assets/hello.html" : url);
            }
        });
    }

    private void initTitleBar() {
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initTitle("网络请求设置");
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
